package cc.wsyw126.java.collection.map;

import java.util.*;

/**
 * Created by junpeng.yang on 16/8/29.
 */
public class KeyEntry {
    public HashMap<Integer, String> getMap(int n) {
        HashMap<Integer, String> map = new HashMap<>();
        Random random = new Random(System.currentTimeMillis());
        for (int i = 0; i < n; i++) {
            map.put(i, random.nextInt(100) + "name");
        }
        return map;
    }

    public void doKey() {
        HashMap<Integer, String> map = getMap(10000000);
        Iterator iter = map.keySet().iterator();
        System.out.println("doKey run time :");
        long start = System.currentTimeMillis();
        while (iter.hasNext()) {
            Object key = iter.next();
            Object val = map.get(key);
        }
        System.out.println("(System.currentTimeMillis() - start) = " + (System.currentTimeMillis() - start));
    }

    public void doEntry() {
        HashMap<Integer, String> map = getMap(10000000);
        Iterator iter = map.entrySet().iterator();
        System.out.println("doEntry run time :");
        long start = System.currentTimeMillis();
        while (iter.hasNext()) {
            Map.Entry entry = (Map.Entry) iter.next();
            Object key = entry.getKey();
            Object val = entry.getValue();
        }
        System.out.println("(System.currentTimeMillis() - start) = " + (System.currentTimeMillis() - start));
    }

    public void doValue() {
        HashMap<Integer, String> map = getMap(10000000);
        Collection<String> collection = map.values();
        Iterator iter = collection.iterator();
        System.out.println("doValue run time :");
        long start = System.currentTimeMillis();
        while (iter.hasNext()) {
            String s = (String) iter.next();
        }
        System.out.println("(System.currentTimeMillis() - start) = " + (System.currentTimeMillis() - start));
    }

    public static void main(String[] args) {
        KeyEntry keyEntry = new KeyEntry();
        keyEntry.doEntry();
        keyEntry.doKey();
        keyEntry.doValue();
    }
}
